//
//  SEGServiceViewController.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/4/12.
//  Copyright © 2018年 刘山. All rights reserved.
//
#import "SEGMenuListView.h"
#import "SEGServiceViewController.h"

@interface SEGServiceViewController ()<UIScrollViewDelegate>
@property (nonatomic, assign) __block BOOL isFirstLoadData;
/**
 大标题导航栏
 */

@property (nonatomic, assign) SEGEnumOpendoorShowType doorShowType;

@end

@implementation SEGServiceViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initParameter];
    [self initNotification];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    [self segShowOrHide];
    [self updateDoorData];
    // 签到气泡
    [self segIsSign];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //判断是否显示活动抽奖气泡 必须放在viewDidAppear里，否则有可能判断当前界面的时候失败
    [self seg_checkActivityLotteryBubble];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 隐藏签到
    [[SEGUhomeMediator sharedInstance] seg_hiddenActivitySignBubble:nil];
    // 隐藏抽奖
    [[SEGUhomeMediator sharedInstance] seg_hiddenActivityLotteryBubble:nil];
}

#pragma mark - init

- (void)initParameter {
    self.isFirstLoadData = YES;
    self.doorShowType = [SEGProfileManager openDoorType];
}

- (void)initNotification {
    //收到活动抽奖推送
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(seg_checkActivityLotteryBubble)
                                                 name:SEGActivityLotteryNotification
                                               object:nil];
}

#pragma mark - send request
- (void)segResponseMenuListData {
    __block BOOL hasAd = NO;//判断是否有广告菜单
    [self.menuDataSource enumerateObjectsUsingBlock:^(NSArray<SEGMenuListViewLayout *> * _Nonnull objSec, NSUInteger idxSec, BOOL * _Nonnull stopSec) {
        [objSec enumerateObjectsUsingBlock:^(SEGMenuListViewLayout * _Nonnull objRow, NSUInteger idxRow, BOOL * _Nonnull stopRow) {
            if(objRow.type == SEGEnumShowTypeAD){
                hasAd = YES;
                *stopRow = YES;
                *stopSec = YES;
            }
        }];
    }];
    [[NSUserDefaults standardUserDefaults]setObject:@(!hasAd) forKey:@"SEGUserDefaultsNavigationBarShow"];
}

/// 判断是否显示活动抽奖气泡
- (void)seg_checkActivityLotteryBubble {
    [[SEGUhomeMediator sharedInstance] seg_checkActivityLotteryBubble:@{@"vc" : self}];
}

//请求活动签到气泡是否显示
- (void)segIsSign {
    [[SEGUhomeMediator sharedInstance] seg_checkActivitySignBubble:@{@"nav" : self.navigationController, @"view" : self.view}];
}
#pragma mark - custom delegate

#pragma mark - scrollViewDelegate  只允许下拉加载
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
}

#pragma mark - event response

#pragma mark - private methods
- (void)refreshOtherData {
    [super refreshOtherData];
    [self updateDoorData];
}

/**
 更新门禁控件数据
 */
- (void)updateDoorData {
    WeakSelf;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.doorControlIndexPath) {
            SEGEnumOpendoorShowType type = [SEGProfileManager openDoorType];
            // 每次进入服务页面的时候，如果存在doorControlCellIndex，并且当前状态是有门禁的，则去调用方法刷新数据
            if (type != SEGEnumOpendoorShowType_NoCooperation && type != SEGEnumOpendoorShowType_NoCertification) {
                [[SEGUhomeMediator sharedInstance] seg_getRecentDoorListWithCallBack:^(NSArray *doorList) {
                    // 对当前菜单数据遍历，赋值门禁列表
                    for (NSArray<SEGMenuListModel *> *sectionData in self.menuDataSource) {
                        for (SEGMenuListModel *submenu in sectionData) {
                            if (submenu.widgetType.integerValue == SEGEnumShowTypeDoorAccessControl) {
                                submenu.doorListData = doorList;
                                break;
                            }
                        }
                    }
                    
                    dispatch_async_on_main_queue(^{
                        // 第一次全局刷新，之后只刷新指定的行
                        if (weakSelf.isFirstLoadData) {
                            [weakSelf.tableView endUpdates];
                            [CATransaction setDisableActions:YES];
                            [weakSelf.tableView reloadData];
                            [CATransaction commit];
                        } else {
                            [weakSelf.tableView endUpdates];
                            [CATransaction setDisableActions:YES];
                            [weakSelf.tableView reloadRowsAtIndexPaths:@[weakSelf.doorControlIndexPath] withRowAnimation:UITableViewRowAnimationNone];
                            [CATransaction commit];
                        }
                        weakSelf.isFirstLoadData = NO;
                    });
                }];
                // 上报之前的开门数据
                [[SEGUhomeMediator sharedInstance] seg_submitOpenDoorRecordHistory];
            } else {
                // 如果当前的状态和之前不一样也要更新门禁cell
                if (self.doorShowType != type) {
                    [CATransaction setDisableActions:YES];
                    [weakSelf.tableView reloadData];
                    [CATransaction commit];
                }
            }
            weakSelf.doorShowType = type;
        }
    });
}
#pragma mark - setter and getter



@end

